﻿using System;
using System.Data;
using BLL;
using Ext.Net;
using DAL;
using Roles;

namespace ERP.Modules
{
    public partial class RecruitmentPlan : System.Web.UI.UserControl
    {
        #region[Define]
        private static bool _flag;
        #endregion

        #region[Controller]
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!X.IsAjaxRequest)
            {
                CheckPermisstion();
                GetRecruitmentPlan();
                btnEdit.Disable(true);
                btnDel.Disable(true);
                btnExport.Disable(true);
            }
        }
        //Store
        protected void StoreRecruitmentPlan_RefreshData(object sender, StoreReadDataEventArgs e)
        {
            txtSearch.Text = string.Empty;
            GetRecruitmentPlan();
        }
        //Button
        protected void btnAddNew_Click(object sender, DirectEventArgs e)
        {
            _flag = true;
            ClearText();
            WindowsRecruitmentPlan.Show();
        }
        protected void btnEdit_Click(object sender, DirectEventArgs e)
        {
            var sm = grRecruitmentPlan.GetSelectionModel() as CheckboxSelectionModel;
            if (sm != null)
                foreach (var row in sm.SelectedRows)
                {
                    _flag = false;
                    GetRecruitmentPlanEdit(int.Parse(row.RecordID));
                    WindowsRecruitmentPlan.Show();
                }
        }
        protected void btnDel_Click(object sender, DirectEventArgs e)
        {
            var sm = grRecruitmentPlan.GetSelectionModel() as CheckboxSelectionModel;
            if (sm != null)
                foreach (var row in sm.SelectedRows)
                {
                    try
                    {
                        var obj = new EtRecruitmentPlan { Plan_ID = int.Parse(row.RecordID) };
                        BllRecruitmentPlan.Delete(obj);
                    }
                    catch (Exception)
                    {
                        HTML.clsFormat.Alert_ERROR("Thông Báo", "Tồn tại tài liệu con, không xoá được vui lòng xoá tài liệu con trước !");
                    }
                }
            GetRecruitmentPlan();
        }
        protected void btnSavePopUp_Click(object sender, DirectEventArgs e)
        {
            Insert();
        }
        protected void btnCancelPopUp_Click(object sender, DirectEventArgs e)
        {
            WindowsRecruitmentPlan.Close();
        }
        //Double-Click
        protected void RecruitmentPlanDblClick(object sender, DirectEventArgs e)
        {
            _flag = false;
            int Plan_ID = int.Parse(e.ExtraParams["Plan_ID"]);
            GetRecruitmentPlanEdit(Plan_ID);
            WindowsRecruitmentPlan.Show();
        }
        //Search
        protected void Search_Event(object sender, DirectEventArgs e)
        {
            var _clsGetBarCode = new BllBarCode();
            var _dtSearch = _clsGetBarCode.GetBarCode(txtSearch.Text);
            if (_dtSearch != null && _dtSearch.Rows.Count > 0)
            {
                StoreRecruitmentPlan.DataSource = _dtSearch;
                StoreRecruitmentPlan.DataBind();
            }
            else
            {
                grRecruitmentPlan.GetStore().RemoveAll();
            }
        }
        #endregion

        #region[Method]
        private void GetRecruitmentPlan()
        {
            var _clsGetRecruitmentPlan = new BllRecruitmentPlan();
            var _dtGetRecruitmentPlan = _clsGetRecruitmentPlan.GetRecruitmentPlan(txtSearch.Text);
            if (_dtGetRecruitmentPlan != null && _dtGetRecruitmentPlan.Rows.Count > 0)
            {
                StoreRecruitmentPlan.DataSource = _dtGetRecruitmentPlan;
                StoreRecruitmentPlan.DataBind();
            }
            else
            {
                grRecruitmentPlan.GetStore().RemoveAll();
            }
        }
        private void GetRecruitmentPlanEdit(int id)
        {
            var _clsGetRecruitmentPlanEdit = new BllRecruitmentPlan();
            var _dtGetRecruitmentPlanEdit = _clsGetRecruitmentPlanEdit.GetRecruitmentPlanEdit(id);
            if (_dtGetRecruitmentPlanEdit != null && _dtGetRecruitmentPlanEdit.Rows.Count > 0)
            {
                lbID.Text = _dtGetRecruitmentPlanEdit.Rows[0]["Plan_ID"].ToString();
                txtRecruitmentPlan_Alias.Text = _dtGetRecruitmentPlanEdit.Rows[0]["Plan_Code"].ToString();
                txtRecruitmentPlan_Name.Text = _dtGetRecruitmentPlanEdit.Rows[0]["Plan_Name"].ToString();
                txtRecruitmentPlan_Note.Text = _dtGetRecruitmentPlanEdit.Rows[0]["Note"].ToString();
                if ((bool)_dtGetRecruitmentPlanEdit.Rows[0]["IsActive"])
                {
                    chkStatus.Checked = true;
                }
                else
                {
                    chkStatus.Checked = false;
                }
            }
        }
        private bool CheckInsert()
        {
            var check = false;
            var _clsGetRecruitmentPlanCheck = new BllRecruitmentPlan();
            var dtCheck = _clsGetRecruitmentPlanCheck.GetRecruitmentPlan(string.Empty);
            if (dtCheck != null && dtCheck.Rows.Count > 0)
            {
                foreach (DataRow dr in dtCheck.Rows)
                {
                    if (lbID.Text != dr["Plan_ID"].ToString())
                    {
                        if (txtRecruitmentPlan_Alias.Text == dr["Plan_Code"].ToString())
                        {
                            check = true;
                            HTML.clsFormat.Alert_ERROR("Thông Báo", "Mã lý do tuyển dụng này " + txtRecruitmentPlan_Alias.Text + " này đã tồn tại !");
                        }
                    }
                }
            }
            return check;
        }
        private void Insert()
        {
            if (_flag)
            {
                InsertValue();
            }
            else
            {
                if (CheckPermisstionDobclick())
                {
                    HTML.clsFormat.Alert_ERROR("Thông Báo", "Bạn không được cấp quyền để sửa đổi !");
                }
                else
                {
                    UpdateValue();
                }
            }
        }
        private void InsertValue()
        {
            try
            {
                if (!CheckInsert())
                {
                    var obj = new EtRecruitmentPlan
                    {
                        Plan_Code = txtRecruitmentPlan_Alias.Text,
                        Plan_Name = txtRecruitmentPlan_Name.Text,
                        IsActive= chkStatus.Checked,
                        Note = txtRecruitmentPlan_Note.Text,
                    };
                    BllRecruitmentPlan.Insert(obj);
                    HTML.clsFormat.Alert_INFO("Thông Báo", "Thêm lý do tuyển dụng thành công !");
                    GetRecruitmentPlan();
                    ClearText();
                }
            }
            catch (Exception ex)
            {
                HTML.clsFormat.Alert_ERROR("Lỗi", ex.Message);
            }
        }
        private void UpdateValue()
        {
            try
            {
                if (!CheckInsert())
                {
                    var obj = new EtRecruitmentPlan
                    {
                        Plan_ID = int.Parse(lbID.Text),
                        Plan_Code = txtRecruitmentPlan_Alias.Text,
                        Plan_Name = txtRecruitmentPlan_Name.Text,
                        IsActive = chkStatus.Checked,
                        Note = txtRecruitmentPlan_Note.Text,
                    };
                    BllRecruitmentPlan.Update(obj);
                    HTML.clsFormat.Alert_INFO("Thông Báo", "Cập nhật lý do tuyển dụng thành công !");
                    GetRecruitmentPlan();
                }
            }
            catch (Exception ex)
            {
                HTML.clsFormat.Alert_ERROR("Lỗi", ex.ToString());
            }
        }
        #endregion

        #region[AutoCode]
        private string AutoCode()
        {
            int i;
            var _clsGetRecruitmentPlan = new BllRecruitmentPlan();
            var dt = _clsGetRecruitmentPlan.GetRecruitmentPlan(string.Empty);
            if (dt != null && dt.Rows.Count > 0)
            {
                int max = int.Parse(dt.Rows[0]["Plan_ID"].ToString());
                for (i = 0; i < dt.Rows.Count; i++)
                {
                    if (int.Parse(dt.Rows[i]["Plan_ID"].ToString()) > max)
                    {
                        max = int.Parse(dt.Rows[i]["Plan_ID"].ToString());
                    }
                }
                i = max + 1;
            }
            else
            {
                i = 0;
            }
            return "MAKH_00" + i;
        }
        #endregion

        #region[ClearText]
        private void ClearText()
        {
            FormPanelRecruitmentPlan.Reset();
            txtRecruitmentPlan_Alias.Text = AutoCode();
            lbID.Text = string.Empty;
        }
        #endregion

        #region[Permisstion]
        private void CheckPermisstion()
        {
            if (Session["login"] != null && Session["USer_Detail_Id"].ToString() != string.Empty)
            {
                bool Is_BtnAdd;
                bool Is_BtnEdit;
                bool Is_BtnView;
                bool Is_BtnDel;
                bool Is_BtnPrint;
                bool Is_BtnCancel;
                bool Is_BtnReview;

                SecurityServices.HasPermisstion_Btn(StringContent.RecruitmentPlan, out Is_BtnAdd, out Is_BtnEdit, out Is_BtnDel, out Is_BtnView, out Is_BtnCancel, out Is_BtnPrint, out Is_BtnReview);
                if (Is_BtnAdd == false)
                {
                    btnAddNew.Visible = false;
                }
                if (Is_BtnEdit == false)
                {
                    btnEdit.Visible = false;
                }
                if (Is_BtnView == false)
                {
                    PanelRecruitmentPlan.Visible = false;
                }
                if (Is_BtnDel == false)
                {
                    btnDel.Visible = false;
                }
                if (Is_BtnPrint == false)
                {
                    btnExport.Visible = false;
                }
            }
        }
        private bool CheckPermisstionDobclick()
        {
            bool check = false;
            if (Session["login"] != null && Session["USer_Detail_Id"].ToString() != string.Empty)
            {
                bool Is_BtnAdd;
                bool Is_BtnEdit;
                bool Is_BtnView;
                bool Is_BtnDel;
                bool Is_BtnPrint;
                bool Is_BtnCancel;
                bool Is_BtnReview;

                SecurityServices.HasPermisstion_Btn(StringContent.RecruitmentPlan, out Is_BtnAdd, out Is_BtnEdit, out Is_BtnDel, out Is_BtnView, out Is_BtnCancel, out Is_BtnPrint, out Is_BtnReview);
                if (Is_BtnEdit == false)
                {
                    check = true;
                }
            }
            return check;
        }
        #endregion
    }
}